const dayjs = require('dayjs');
const app = getApp();
Page({
    data: {
        navBarHeight: 0,
        currentTab: 0,
        tabs: ['嘟嘟城际', '企业班线', '嘟嘟包车', '关于嘟嘟'],
        hotLine: [
            { id: 1, name: '广州', },
            { id: 2, name: '深圳', },
            { id: 3, name: '东莞', },
            { id: 4, name: '佛山', },
            { id: 5, name: '惠州', },
            { id: 6, name: '珠海', },
            { id: 7, name: '江门', }
        ],
        hotRouteList: [
            { from: '深圳', to: '广州', price: 28 },
            { from: '深圳', to: '东莞', price: 28 },
            { from: '深圳', to: '惠州', price: 28 },
            { from: '深圳', to: '佛山', price: 28 },
            { from: '深圳', to: '珠海', price: 28 },
            { from: '深圳', to: '中山', price: 28 },
            { from: '深圳', to: '江门', price: 28 }
        ],
        hotRouteLoading: false,
        showTime: false,
        dateTime: '',
        fieldNames: {
            text: 'value',
            value: 'text',
            children: 'children',
        },
        cityStartList: [],
        showStartCity: false,
        cityStartValue: { text: null, value: null },
        areaStartValue: { text: null, value: null },
        cityEndList: [],
        showEndCity: false,
        cityEndValue: { text: null, value: null },
        areaEndValue: { text: null, value: null },
    },

    onLoad: function (options) {
        const systemInfo = wx.getSystemInfoSync();
        const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
        const navBarHeight = (menuButtonInfo.top - systemInfo.statusBarHeight) * 2 + menuButtonInfo.height;
        const totalTopHeight = systemInfo.statusBarHeight + navBarHeight;

        this.setData({
            statusBarHeight: wx.getSystemInfoSync()?.statusBarHeight || 0,
            navBarHeight: totalTopHeight
        })
        this.initDate();
        this.queryHotRoutes();
        this.queryCityList(1);

    },

    // 初始化日期
    initDate: function () {
        const now = dayjs().format('YYYY-MM-DD');
        this.setData({ dateTime: now, });
    },
    // 查询热门路线
    queryHotRoutes: async function () {
        try {
            this.setData({ hotRouteLoading: true });
            const res = await app.request({ url: '/sitebus/line/hot_lines' });
            this.setData({
                hotRouteList: res.data
            });
        } catch (err) {
            wx.showToast({
                title: err.message || '加载失败',
                icon: 'none'
            });
        } finally {
            this.setData({ hotRouteLoading: false });
        }
    },
    // 查询城市列表
    queryCityList: async function (type) {
        try {
            const res1 = await app.request({ url: '/sitebus/station/get_station_city', data: { "type": 1 } });
            const res2 = await app.request({ url: '/sitebus/station/get_station_city', data: { "type": 2 } });
            this.setData({
                cityStartList: res1.data,
                cityEndList: res2.data
            });
        } catch (err) {
            wx.showToast({
                title: err.message || '加载失败',
                icon: 'none'
            });
        } finally {
        }

    },

    switchTab: function (e) {
        const index = e.currentTarget.dataset.index;
        console.log(e.currentTarget.dataset.index, 'e.currentTarget.dataset.index');
        if (index == 1) {
            wx.navigateTo({
                url: '/pages/sitebus/enterprise_bus_line/enterprise_bus_line?link=4'
            })
            return
        } else if (index == 2) {
            wx.navigateTo({
                url: '/pages/sitebus/enterprise_bus_line/enterprise_bus_line?link=2'
            })
            return
        } else if (index == 3) {
            wx.navigateTo({
                url: '/pages/duduindex/duduindex?isVisble=true'
            })
            return
        }
        if (this.data.currentTab !== index) {
            this.setData({
                currentTab: index
            });
        }
    },

    onClickStartCity() {
        this.setData({
            showStartCity: true,
        });
    },

    onCloseStartCity() {
        this.setData({
            showStartCity: false,
        });
    },

    onFinishStartCity(e) {
        const { selectedOptions, value } = e.detail;
        const fieldValue = selectedOptions[1]
        const fieldValue2 = selectedOptions[2]
        this.setData({
            cityStartValue: fieldValue,
            cascaderStartValue: value,
            areaStartValue: fieldValue2,
        })
        setTimeout(() => {
            this.onCloseStartCity();
        }, 100);
    },


    onClickEndCity() {
        this.setData({
            showEndCity: true,
        });
    },

    onCloseEndCity() {
        this.setData({
            showEndCity: false,
        });
    },

    onFinishEndCity(e) {
        const { selectedOptions, value } = e.detail;
        const fieldValue = selectedOptions[1]
        const fieldValue2 = selectedOptions[2]
        this.setData({
            cityEndValue: fieldValue,
            cascaderEndValue: value,
            areaEndValue: fieldValue2,
        })
        setTimeout(() => {
            this.onCloseEndCity();
        }, 100);
    },
    // 城市交换
    citySwitch: function () {
        this.setData({
            cityStartValue: this.data.cityEndValue,
            cityEndValue: this.data.cityStartValue,
            areaStartValue: this.data.areaEndValue,
            areaEndValue: this.data.areaStartValue,
        })

    },


    onTimeDisplay() {
        this.setData({ showTime: true });
    }
    ,
    onTimeClose() {
        this.setData({ showTime: false });
    }
    ,
    onTimeConfirm: function (event) {
        let date = dayjs(event.detail).format('YYYY-MM-DD');

        this.setData({
            showTime: false,
            dateTime: date,
        });
    }
    ,

    // 搜索班次
    searchSchedules: function () {
        const { cityStartValue, cityEndValue, dateTime } = this.data;

        if (!cityStartValue.text || !cityEndValue.text) {
            wx.showToast({
                title: '请选择出发城市和到达城市',
                icon: 'none'
            });
            return;
        }
        if (!dateTime) {
            wx.showToast({
                title: '请选择日期',
                icon: 'none'
            });
            return;
        }
        // 跳转到班次列表页
        wx.navigateTo({
            url: `/pages/sitebus/trip/trip-list`,
            success: function (res) {
                res.eventChannel.emit('acceptDataFromHomePage', {
                    start: cityStartValue.text,
                    end: cityEndValue.text,
                    dateTime: dateTime
                });
            }
        });
    }
    ,

    // 选择热门路线
    selectHotRoute: function (e) {
        const route = e.currentTarget.dataset.route;
        this.setData({
            fromCity: route.from,
            toCity: route.to
        });

        // 自动搜索
        this.searchSchedules();
    }


})
    ;